package com.api.phm.controller;

import com.alibaba.fastjson2.JSONObject;
import com.api.common.core.controller.BaseController;
import com.api.common.core.domain.AjaxResult;
import com.api.common.core.page.TableDataInfo;
import com.api.common.core.redis.RedisCache;
import com.api.common.utils.MessageUtils;
import com.api.phm.domain.vo.AlarmLogVo;
import com.api.phm.domain.vo.AlarmRuleQueryListVo;
import com.api.phm.domain.vo.AlarmRuleVo;
import com.api.phm.service.IAlarmRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/hscn/rule")
public class AlarmRuleController extends BaseController {

    @Autowired
    private IAlarmRuleService alarmRuleService;

    @PostMapping("/list")
    public TableDataInfo searchList(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmRuleVo alarmRuleVo) {
        startPageByPost(alarmRuleVo.getPageNum(), alarmRuleVo.getPageSize());
        alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
        return getDataTable(alarmRuleService.searchList(alarmRuleVo));
    }

    /**
     * ## 规则列表查询采集器示例
     * post
     * {
     * "ruleId": 1,
     * "ruleName": "采集器1设备1部件1波形3和波形6规则名称",
     * "ruleType": 3,
     * "ruleMold": "PLC",			## PLC采集器类型和原装备，STC电堆类
     * "deviceMap": {
     * "plcId": 1,
     * "robotId": 1,
     * "elementId": 1,
     * "datCol": 3
     * },
     * "enable": "Y"				## 是否使用
     * }
     *
     * @param appLang
     * @param alarmRuleVo
     * @return
     */
    @PostMapping("/list2")
    public TableDataInfo searchList2(@RequestHeader("App-Language") String appLang
            , @RequestBody @Valid AlarmRuleQueryListVo alarmRuleVo) {
        startPageByPost(alarmRuleVo.getPageNum(), alarmRuleVo.getPageSize());
        alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
//        return getDataTable(alarmRuleService.searchList(alarmRuleVo));
        return getDataTable(alarmRuleService.searchList2(alarmRuleVo));
    }

    @PostMapping("/log/list")
    public TableDataInfo searchAlarmLogList(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmLogVo alarmLogVo) {
        System.out.println("searchPlcList appLang:" + appLang);
        startPageByPost(alarmLogVo.getPageNum(), alarmLogVo.getPageSize());
//        alarmLogVo.initControl(getLoginUser().getUser().getUserName(), appLang);
        return getDataTable(alarmRuleService.searchAlarmLog(alarmLogVo));
    }

    @PostMapping("/info")
    public AjaxResult searchItem(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmRuleVo alarmRuleVo) {
        try {
            alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("data", alarmRuleService.searchItem(alarmRuleVo));
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error(MessageUtils.message(appLang, "common.run.error"));
        }
    }

    @PostMapping("/log/info")
    public AjaxResult searchAlarmLogInfo(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmLogVo alarmLogVo) {
        try {
//            alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("data", alarmRuleService.searchAlarmLogItem(alarmLogVo));
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error(MessageUtils.message(appLang, "common.run.error"));
        }
    }

    @PostMapping("/add")
    public AjaxResult addRule(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmRuleVo alarmRuleVo) {
        try {
            alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
            AjaxResult ajaxResult = AjaxResult.success();
            ajaxResult.put("data", alarmRuleService.add(alarmRuleVo));
            return ajaxResult;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(MessageUtils.message(appLang, "common.run.error"));
        }
    }

    @PostMapping("/modify")
    public AjaxResult updateInfo(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmRuleVo alarmRuleVo) {
        try {
            alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
            AjaxResult ajaxResult = AjaxResult.success();
            alarmRuleService.update(alarmRuleVo);
//            ajaxResult.put("data", alarmRuleService.searchItem(alarmRuleVo));
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error(MessageUtils.message(appLang, "common.run.error"));
        }
    }

    @PostMapping("/log/readMarkModify")
    public AjaxResult updateLogReadMark(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmLogVo alarmLogVo) {
        try {
//            alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
            AjaxResult ajaxResult = AjaxResult.success();
            alarmRuleService.updateAlarmReadMark(alarmLogVo.getAlarmId());
            ajaxResult.put("data", "");
            return ajaxResult;
        } catch (Exception e) {
            return AjaxResult.error(MessageUtils.message(appLang, "common.run.error"));
        }
    }

    @PostMapping("/remove")
    public AjaxResult remove(@RequestHeader("App-Language") String appLang
            , @RequestBody AlarmRuleVo alarmRuleVo) {
        try {
            alarmRuleVo.initControl(getLoginUser().getUser().getUserName(), appLang);
            alarmRuleService.delete(alarmRuleVo.getRuleId());
            return AjaxResult.success("");
        } catch (Exception e) {
            return AjaxResult.error(MessageUtils.message(appLang, "common.run.error"));
        }
    }

    @Autowired
    private RedisCache redisCache;

    @PostMapping("/test")
    public AjaxResult test(@RequestHeader("App-Language") String appLang
            , @RequestBody JSONObject jo) {
//        alarmRuleService.refreshCacheRule();
//        alarmRuleService.checkAllRule();
        Map<String, String> m = new HashMap<>();
        m.put("setCacheMap", "1");
        redisCache.setCacheMap("setCacheMap", m);
//        redisCache.set

        return AjaxResult.success("完成");
    }
}
